package com.company.OfferGuide;

import java.util.HashMap;

public class 在排序数组中查找数字 {

    public static void main(String[] args) {
        System.out.println(search(new int[]{1}, 1));
    }

    public static int search(int[] nums, int target) {
        int left = 0,right = nums.length - 1;
        int res = 0;
        while (left >=0 && right <= nums.length - 1 && left <= right) {
            int mid = (left + right) /2;
            if (nums[mid] < target) {
                left = mid + 1;
            } else if (nums[mid] > target) {
                right = mid - 1;
            } else {
                res++;
                int _left = mid - 1;
                int _right = mid + 1;
                while (_left >= 0 && nums[_left] == target) {
                    res++;
                    _left--;
                }
                while (_right <= nums.length - 1 && nums[_right] == target) {
                    res++;
                    _right++;
                }
                break;
            }
        }

        return res;
    }

}
